package com.pearadmin.pro.modules.ta.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.tools.core.GeneralConvertUtils;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;
import com.pearadmin.pro.modules.project.domain.Project;
import com.pearadmin.pro.modules.sys.domain.BusinessFile;
import com.pearadmin.pro.modules.sys.service.BusinessFileService;
import com.pearadmin.pro.modules.ta.domain.TA;
import com.pearadmin.pro.modules.ta.dto.TaDTO;
import com.pearadmin.pro.modules.ta.param.TARequest;
import com.pearadmin.pro.modules.ta.repository.TARepository;
import com.pearadmin.pro.modules.ta.service.TAService;
import com.pearadmin.pro.modules.ta.vo.TaVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * taService业务层处理
 *
 * @author ChenJian
 * @date 2021-08-22
 */
@Service
public class TAServiceImpl extends ServiceImpl<TARepository, TA> implements TAService
{
    @Resource
    private TARepository tARepository;
    @Resource
    private BusinessFileService businessFileService;
    /**
     * 查询ta
     * @param request
     * @return ta 分页集合
     * */
    @Override
    public PageResponse<TaVo> page(TARequest request)
    {
        LambdaQueryWrapper<TA> query = new LambdaQueryWrapper<TA>();
        query.like(ObjectUtil.isNotEmpty(request.getTaName()),TA::getTaName,request.getTaName());
        query.like(ObjectUtil.isNotEmpty(request.getTaMajor()),TA::getTaMajor,request.getTaMajor());
        query.like(ObjectUtil.isNotEmpty(request.getSchName()),TA::getSchName,request.getSchName());
        query.like(ObjectUtil.isNotEmpty(request.getCreateBy()),TA::getCreateBy,request.getCreateBy());
        query.ge(ObjectUtil.isNotEmpty(request.getStatCreateTime()),TA::getCreateTime,request.getStatCreateTime());
        query.le(ObjectUtil.isNotEmpty(request.getEndCreateTime()),TA::getCreateTime,request.getEndCreateTime());
        return Pageable.of(request,(()->  tARepository.selectList(query)));
    }

    @Override
    public Boolean saveOrUpdate(TaDTO ta) {
        TA tae = GeneralConvertUtils.conv(ta, TA.class);
        if (!ObjectUtils.isEmpty(ta.getFiles())) {
            //保存附件
            List<BusinessFile> businessFileList = GeneralConvertUtils.convert2List(ta.getFiles(), BusinessFile.class);
            businessFileList.stream().forEach(s -> {
                s.setBusinessId(tae.getId());
            });
            businessFileService.saveOrUpdateBatch(businessFileList);
        }
        return saveOrUpdate(tae);
    }

    /**
     * 查询ta
     *
     * @param id taID
     * @return ta;
     */
    @Override
    public TaDTO selectTAById(Long id)
    {

        TaDTO ta = GeneralConvertUtils.conv(this.getById(id),TaDTO.class);
        LambdaQueryWrapper<BusinessFile> queryBusinessFile = new LambdaQueryWrapper<>();
        queryBusinessFile.eq(BusinessFile::getBusinessId, id);
        ta.setFiles(businessFileService.list(queryBusinessFile));

        return ta;
    }

}
